package org.record.mood.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.record.mood.bean.Message;
import org.record.mood.bean.Sort;
import org.record.mood.dao.MessageMapper;
import org.record.mood.dao.SortMapper;
import org.record.mood.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: chaihf
 * @version:1.0.0
 * @time: 2020-06-22 11:10
 */
@Service
@Slf4j
public class TestServiceImpl implements TestService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    public static final String MS_LOCK_KEY = "MSKEYLOCK";
    @Autowired
    private MessageMapper messageMapper;
    private static boolean flag = true;


    @Override
    @Transactional
    public String insert(String key) throws InterruptedException {
        try {
            if (!flag) {
                log.debug("产品已经销售一空");
                return "产品已销售一空";
            }
            int message = messageMapper.countMessage();
            log.debug("当前产品数量还有[{}]个", message);
            if (message <= 0) {
                flag = false;
                log.debug("产品已经销售一空");
                return "产品已销售一空";
            } else {
                int i = messageMapper.updateMessage();
                if (i > 0) {
                    return "购买成功";
                } else {
                    return "失败";
                }
            }
        } finally {
            log.debug("执行完成释放锁");
            stringRedisTemplate.delete(MS_LOCK_KEY);
        }
    }

}
